#!/bin/sh
# credit to uginyaa

SPEED="$1"

CACHE_DIR="$HOME/.cache/sb-transfer"

PREV_RX_FILE="$CACHE_DIR/rx_bytes"
PREV_TX_FILE="$CACHE_DIR/tx_bytes"
CURR_RX_FILE="/sys/class/net/enp4s0/statistics/rx_bytes"
CURR_TX_FILE="/sys/class/net/enp4s0/statistics/tx_bytes"

mkdir -p "$CACHE_DIR"

[ -e "$PREV_RX_FILE" ] || cat "$CURR_RX_FILE" > "$PREV_RX_FILE"
[ -e "$PREV_TX_FILE" ] || cat "$CURR_TX_FILE" > "$PREV_TX_FILE"

PREV_RX=$(cat "$PREV_RX_FILE")
PREV_TX=$(cat "$PREV_TX_FILE")
CURR_RX=$(cat "$CURR_RX_FILE")
CURR_TX=$(cat "$CURR_TX_FILE")

SPEED_RX=$(( $((CURR_RX - PREV_RX)) / SPEED ))
SPEED_TX=$(( $((CURR_TX - PREV_TX)) / SPEED ))

HUM_SPEED_RX=$(echo "$SPEED_RX" | numfmt --to=iec --suffix=B)
HUM_SPEED_TX=$(echo "$SPEED_TX" | numfmt --to=iec --suffix=B)

printf " %5s  %5s\n" "$HUM_SPEED_RX" "$HUM_SPEED_TX"

echo "$CURR_RX" > "$PREV_RX_FILE"
echo "$CURR_TX" > "$PREV_TX_FILE"
